
package de.justphil.tcg.tcgandroidclient.util;

import java.io.UnsupportedEncodingException;

import android.util.Base64;

import com.twmacinta.util.MD5;

public class GeoCalculation {
	
	// 51.346054/6852493
	private static final int DOUBLE_TO_INT_FACTOR = 1000000;
	private static final String PRIVATE_KEY = "#50*Dead*Men*Walking#";
	
	// /*
	// * Get the GEO distance between two GPS fixes in kilometers.
	// */
	// public double getDistance( GeoLocation g1, GeoLocation g2 ) {
	//
	// final double R = RestConfig.EARTH_RADIUS;
	//
	// double lat1 = Math.toRadians( g1.getLat() );
	// double lon1 = Math.toRadians( g1.getLon() );
	// double lat2 = Math.toRadians( g2.getLat() );
	// double lon2 = Math.toRadians( g2.getLon() );
	//
	// // Spherical Law of Cosines
	// double d = Math.acos(
	// Math.sin( lat1 ) * Math.sin( lat2 ) +
	// Math.cos( lat1 ) * Math.cos( lat2 ) *
	// Math.cos( lon2-lon1 )
	// ) * R;
	//
	// log.debug("d({}, {}) => {}", new Object[] { g1, g2, d });
	//
	// return d;
	// }
	// public static String getGeoString(int geoInt) {
	//
	// double geoDouble = ((double) geoInt) / DOUBLE_TO_INT_FACTOR;
	// return String.valueOf(geoDouble);
	// }
	//
	//
	// public static int getGeoInt(String geoStr) {
	//
	// double geoDouble = Double.valueOf(geoStr);
	// TCGLog.w(new Object[] {geoStr});
	// return (int) (geoDouble * DOUBLE_TO_INT_FACTOR);
	// }
	public static String getLocationHash(double lat, double lon, long cardId) {
	
		/* String $lat = $latitude.toString(); String $lon = $longitude.toString(); String $ciId =
		 * $cardInstanceId.toString(); String $hash = md5( $lat + $lon + $ciId + $tcgPrivateKey ) */

		/*
		 * 51.194364196084146.76789184320125754.0#50*Dead*Men*Walking# => a4c3d04e3559112399bdbceacf8b824f)
		 */
		String combinedStr = StringTools.getString(new Object[] {
				lat, lon, cardId, PRIVATE_KEY });
		MD5 md5 = new MD5();
		try {
			md5.Update(combinedStr, null);
		}
		catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage());
		}
		String hash = md5.asHex();
		TCGLog.d(new Object[] {
				"Calculated Hash:(", combinedStr, " => ", hash, ")" });
		return hash;
	}
}
